package it.asvp.asvp_portal.web.controller;

import it.asvp.asvp_portal.asvp_util.enumerative.Role;
import it.asvp.asvp_portal.asvp_util.util.Constants;
import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.entity.domain.Address;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.service.services.MemberAccountService;
import it.asvp.asvp_portal.service.services.MemberService;
import it.asvp.asvp_portal.web.bean.MemberAccountBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/memberaccount")
public class MemberAccountController extends BaseController{

	private static final Log log = LogFactory.getLog(MemberAccountController.class);
	
	@Autowired
	MemberService memberService;
	@Autowired
	MemberAccountService memberAccountService;
	
	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public ModelAndView getAllMemberAccounts(HttpServletRequest request, HttpServletResponse response) {
    
		log.info("Accesso alla pagina di gestione degli accounts");
		
		try{
			
		    ModelAndView mv = getModelAndView("memberaccount/default");
		    mv.addObject(Constants.MemberAccount.MEMBER_ACCOUNTS, memberAccountService.getAllAccounts());
		    
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/edit/{idMember}", method = RequestMethod.GET)
	public ModelAndView getMemberDetails(@PathVariable final long idMember) {
    
		log.info("Accesso alla pagina di gestione degli utenti");
		
		try{
			
			ModelAndView mv = getModelAndView("memberaccount/edit");
		    mv.addObject(Constants.MemberAccount.MEMBER, memberService.getMemberById(idMember));
		    
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insertMemberAccount(@ModelAttribute("memberaccountbean")MemberAccountBean memberAccountBean,BindingResult result) {
    
		log.info("Accesso alla pagina di inserimento utenti");
		
		try{
			
			Role currentRole=memberAccountService.getRole(getUserDetail());
			ModelAndView mv = getModelAndView("memberaccount/insert");
			mv.addObject(Constants.MemberAccount.ALL_SEX,memberAccountBean.getAllSex());
			mv.addObject(Constants.MemberAccount.ROLES,memberAccountBean.getRoles(currentRole));
		    
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createMemberAccount(@ModelAttribute("MemberAccountBean")MemberAccountBean memberAccountBean,BindingResult result,HttpServletRequest request) {
    
		log.info("Accesso alla pagina di inserimento utenti");
		
		try{
			
//			Role role=Role.getRoleByName(request.getParameter("role"));
			// validazione
			Address address=memberAccountBean.getAddress();
			Member member=memberAccountBean.getMember();
			member.setAddress(address);
			
			//FIXME: Da sostituire con la data ricavata dalla form di inserimento
			member.setBirthDate(DateUtils.getCurrentDate());
			
			it.asvp.asvp_portal.data.entity.member.MemberAccount memberAccount=memberAccountBean.getMemberAccount();
			
			//FIXME: Da sostituire con la data ricavata dalla form di inserimento
			memberAccount.setExpireDate(DateUtils.getCurrentDate());
			
//			memberAccount.setRole(role);
			
			memberService.createMember(getUserDetail(),member, memberAccount);
			return new ModelAndView(Constants.REDIRECT+"/memberaccount/default");
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	
	
}
